<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/calendar/CalendarUtil.class.php
 *
 * 日历工具
 *
 * PHP Version 5
 *
 * @package:   class.calendar
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once CLASS_PATH.'user/UserUtil.class.php';

include_once FUNCTION_PATH.'utf8_substr.fun.php';

class CalendarUtil {

   /**
    * 取得当日的事件总数
    * @param: &$db 数据库的连接
    * @param:  $year, 年
    * @param:  $month, 月
    * @param:  $day  日
    * @return totoal_number 事件数
    * @access: public
    * @static
    */
   public static function getDayNumber(&$db, $year, $month, $day ) {
      $sql = 'select count(*) as num from calendar where year(createtime)=? and month(createtime)=? '.
         ' and day(createtime)=? ';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($year, $month, $day));
      $rows = $res->FetchRow();

      return $rows['num'];

   }


   /**
    * 取得事件的数量
    * @param: &$db 数据库的连接
    * @param:  $year, 年
    * @param:  $month, 月
    * @param:  $day  日
    * @return totoal_number 事件数
    * @access: public
    * @static
    */
   public static function getDayEventNumber(&$db, $year, $month, $day) {
      $sql = 'select count(*) as num from calendar where year(createtime)=? and month(createtime)=? '.
         ' and day(createtime)=?  and type=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($year, $month, $day, 'event'));
      $rows = $res->FetchRow();

      return $rows['num'];
   }

   public static function getDayDatingNumber(&$db, $year, $month, $day) {
      $sql = 'select count(*) as num from calendar where year(createtime)=? and month(createtime)=? '.
         ' and day(createtime)=?  and type=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($year, $month, $day, 'dating'));
      $rows = $res->FetchRow();

      return $rows['num'];
   }

   public static function getDayPlanNumber(&$db, $year, $month, $day) {
      $sql = 'select count(*) as num from calendar where year(createtime)=? and month(createtime)=? '.
         ' and day(createtime)=?  and type=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($year, $month, $day, 'plan'));
      $rows = $res->FetchRow();

      return $rows['num'];
   }


   /**
    * 取得总的页数
    * @param:  &$db 数据库的连接
    * @param:  $year 年份
    * @param:  $month 月份
    * @param:  $day  天
    * @param:  $pre_page   每页显示的条数
    * @return: $total_page 总的页数
    * @access: public
    * @static
    */
   public static function getTotalPage(&$db, $year, $month, $day, $pre_page = 15) {
      $sql = 'select count(*) as num from calendar where year(createtime)=? and month(createtime)=? '.
         ' and day(createtime)=? ';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($year, $month, $day, 'plan'));
      $rows = $res->FetchRow();
      
      $total_number = $rows['num'];
      
      $total_page = ceil($total_number / $pre_page);
      
      return $total_page;
   }


   /**
    * 返回查询的指定日期日历数据
    * @param:  &$db 数据库连接
    * @param:  $year 年
    * @param:  $month 月
    * @param:  $day  日期
    * @param:  $pre_page   每页显示的数据
    * @param:  $offset_page   偏移的数据
    * @return: $event_info 返回的数据数组
    * @access: public
    * @static
    */
   public static function &getDayData(&$db, $year, $month, $day, $pre_page = 15, $offset_page = 0 ) {
      $sql = 'select id, user_id, type, createtime, fromtime, endtime, emailremind, '.
         'messageremind,  title, accesstype from calendar where year(createtime)=? '.
         ' and month(createtime)=? and day(createtime)=? ';

      $res = $db->SelectLimit($sql, $pre_page, $offset_page, 
         array ( $year, $month, $day ) );

      $event_info = array ();

      while ( $rows = $res->FetchRow() ) {
         $user_name = UserUtil::getUserNameById($db, $rows['user_id']);
         if ( !$user_name ) {
            $user_name = '匿名用户';
         }

         $event_type = '';
         switch($rows['type']) {
         case 'event':
            $event_type = '事件';
            break;
         case 'dating':
            $event_type = '约会';
            break;
         case 'plan':
            $event_type = '计划';
            break;
         }

         $event_info[] = array (
            'id'=>$rows['id'],
            'username'=>$user_name,
            'eventtype'=>$event_type,
            'type'=>$rows['type'],
            'createtime'=>$rows['createtime'],
            'fromtime'=>$rows['fromtime'],
            'endtime'=>$rows['endtime'], 
            'title'=>$rows['title'], 
            'stitle'=>utf8_substr($rows['title'], 0, 13),
            'accesstype'=>$rows['accesstype'] 
         );
      }


      return $event_info;
   }


   /**
    * 取得单个事件的内容
    * @param:  &$db 数据库的连接
    * @param:  $id   事件的id
    * @access: public
    * @static
    */
   public static function &getContent(&$db, $id) {
      $sql = 'select id, user_id, type, createtime, fromtime, endtime, emailremind, '.
         'messageremind,  content, title, accesstype from calendar where id=? ';
      $sth = $db->prepare($sql);
      
      $res = $db->CacheExecute(120, $sql, 
         array ( $id ) );

      $event_info = NULL;

      if ( $rows = $res->FetchRow() ) {
         $user_name = UserUtil::getUserNameById($db, $rows['user_id']);
         if ( !$user_name ) {
            $user_name = '匿名用户';
         }

         $event_type = '';
         switch($rows['type']) {
         case 'event':
            $event_type = '事件';
            break;
         case 'dating':
            $event_type = '约会';
            break;
         case 'plan':
            $event_type = '计划';
            break;
         }

         $event_info= array (
            'id'=>$rows['id'],
            'username'=>$user_name,
            'eventtype'=>$event_type,
            'type'=>$rows['type'],
            'createtime'=>$rows['createtime'],
            'fromtime'=>$rows['fromtime'],
            'endtime'=>$rows['endtime'], 
            'title'=>$rows['title'], 
            'stitle'=>utf8_substr($rows['title'], 0, 13),
            'accesstype'=>$rows['accesstype'],
            'content'=>$rows['content'],
         );
      }

      return $event_info;
   }


}

?>
